package com.example.a14110100420.yuejian.WActivity.picture;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WAdapter.picture.PictureDetailAdapter;
import com.example.a14110100420.yuejian.WSerivice.PictureService;
import com.example.a14110100420.yuejian.Wbase.BaseActivity;
import com.example.a14110100420.yuejian.Wbase.Conast;
import com.example.a14110100420.yuejian.Wbean.picture.PictureDetail;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PictureDetailActivity extends BaseActivity {
    final String reply_userid = getUserid();
    private ImageView touxiang;
    private TextView username;
    private TextView fatie_time;
    private TextView fatie_detail;
    private ImageView fatie_pic;
    private String writes;
    private EditText writeComment;
    private Button fabuComment;
    private RecyclerView recyclerView;

    private String shaitu_id;
    private String userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_detail);
        getSupportActionBar().hide();
        initView();
        getPictureDetail();
        setComment();
    }
    public void initView(){
        fabuComment = (Button) findViewById(R.id.fabu_button);
        writeComment = (EditText) findViewById(R.id.picture_detail_edit_reply);
        touxiang = (ImageView) findViewById(R.id.picture_detail_userpic);
        username = (TextView) findViewById(R.id.picture_detail_username);
        fatie_time = (TextView) findViewById(R.id.picture_detail_fatietime);
        fatie_detail = (TextView) findViewById(R.id.picture_detail_title);
        fatie_pic = (ImageView) findViewById(R.id.picture_detail_fatiepic);
    }
    private void getPictureDetail(){
        Intent intent =getIntent();
        Picasso.with(getBaseContext())
                .load("http://172.24.10.175/workout/Uploads/"+intent.getStringExtra("user_pic"))
                .into(touxiang);
        username.setText(intent.getStringExtra("user_name"));
        fatie_time.setText(intent.getStringExtra("fatie_time"));
        fatie_detail.setText(intent.getStringExtra("fatie_detail"));
        Picasso.with(PictureDetailActivity.this)
                .load("http://172.24.10.175/workout/Uploads/"+intent.getStringExtra("fatiepic"))
                .into(fatie_pic);
        if(fatie_pic.getDrawable()==null){
            fatie_pic.setImageResource(R.drawable.wutouxiang);
        }

        recyclerView = (RecyclerView) findViewById(R.id.picture_detail_rv);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PictureService service = retrofit.create(PictureService.class);
        Call<List<PictureDetail>> call = service.pictureDetail(intent.getStringExtra("shaitu_id"));
        call.enqueue(new Callback<List<PictureDetail>>() {
            @Override
            public void onResponse(Call<List<PictureDetail>> call, Response<List<PictureDetail>> response) {
                List<PictureDetail> pictureDetails = response.body();

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PictureDetailActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                PictureDetailAdapter adapter = new PictureDetailAdapter(PictureDetailActivity.this,pictureDetails);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.setAdapter(adapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFailure(Call<List<PictureDetail>> call, Throwable t) {

            }
        });
    }
    private void setComment(){
        fabuComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String comment =  writeComment.getText().toString();
                if (comment.equals("")){
                    Toast.makeText(PictureDetailActivity.this,"评论内容不能为空",Toast.LENGTH_SHORT).show();}
                else {
                    Intent intent = getIntent();
                    shaitu_id = intent.getStringExtra("shaitu_id");
                    userid = intent.getStringExtra("userid");
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(Conast.base_Url)
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    PictureService pictureService = retrofit.create(PictureService.class);
                    Call<String> call = pictureService.insertcomment(shaitu_id,
                            userid, reply_userid, comment);
                    call.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if(response.isSuccessful())
                                Toast.makeText(PictureDetailActivity.this, "评论成功",
                                        Toast.LENGTH_LONG).show();
                            refresh();
                            writeComment.setText(null);
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {

                        }
                    });


                }

            }
        });

    }
    private void refresh(){
        getPictureDetail();
    }
}
